/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;

import tw.com.hitevision.whiteboard.android.stateFile.StateFileSaver;
import tw.com.hitevision.whiteboard.android.stateFile.StickyNoteStateFile;

public class StickyNote extends BaseItem {
    private final Bitmap bitmap;
    private final Matrix transform;
    private final RectF background;
    private final RectF rectF;

    private String bitmapName;

    public StickyNote(Bitmap bitmap, float x, float y) {
        this.bitmap = bitmap;

        background = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        transform = new Matrix();
        rectF = new RectF();

        transform.setTranslate(x, y);
        setBounds(x, y, bitmap.getWidth(), bitmap.getHeight());

        bitmapName = createBitmapFileName();
    }

    @Override
    public void moveBy(float dx, float dy) {
        super.moveBy(dx, dy);
        transform.postTranslate(dx, dy);
    }

    @Override
    public void scaleFromPoint(float rateX, float rateY, PointF p) {
        transform.postScale(rateX, rateY, p.x, p.y);

        transform.mapRect(rectF, background);
        setBounds(rectF.left, rectF.top, rectF.right - rectF.left, rectF.bottom - rectF.top);
    }

    @Override
    public void rotateFromPoint(float degree, PointF p) {
        transform.postRotate(degree, p.x, p.y);

        transform.mapRect(rectF, background);
        setBounds(rectF.left, rectF.top, rectF.right - rectF.left, rectF.bottom - rectF.top);
    }

    @Override
    public BaseItem copy() {
        final StickyNote copy = new StickyNote(bitmap.copy(bitmap.getConfig(), false), x, y);

        copy.background.set(background);
        copy.transform.set(transform);
        copy.rectF.set(rectF);
        copy.setColor(getColor());
        copy.setBounds(x, y, width, height);

        copy.bitmapName = bitmapName;

        return copy;
    }

    public static BaseItem copyFromStateFile(Context context, String bitmapName, float x, float y, float width, float height,
                                             RectF background, float[] transformValues, RectF rectF, int color) {

        Bitmap bitmap = StateFileSaver.getInstance(context).getJsonImage(bitmapName);

        if (bitmap == null) {
            return null;
        }

        final StickyNote copy = new StickyNote(bitmap, x, y);

        copy.background.set(background);
        copy.transform.setValues(transformValues);
        copy.rectF.set(rectF);
        copy.setColor(color);
        copy.setBounds(x, y, width, height);

        return copy;
    }

    public StickyNoteStateFile getStickyNoteStateFile() {
        return new StickyNoteStateFile(this, background, transform, rectF, bitmapName);
    }

    private String createBitmapFileName() {
        return "stickyNote" + getId() + ".png";
    }

    public String getBitmapFileName() {
        return bitmapName;
    }

    @Override
    public void render(Canvas canvas, Paint paint, Context context) {
        canvas.save();

        canvas.concat(transform);

        // draw background
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getColor());
        canvas.drawRect(background.left, background.top, background.right, background.bottom, paint);

        // draw image
        canvas.drawBitmap(bitmap, 0, 0, paint);

        canvas.restore();
    }

    @Override
    public void recycle() {
        bitmap.recycle();
    }
}
